package com.datang.petstore.service.impl;

import com.datang.petstore.entity.Microblog;
import com.datang.petstore.entity.MicroblogShareRecord;
import com.datang.petstore.exception.AcceptResultCodeException;
import com.datang.petstore.generatemapper.MicroblogShareRecordMapper;
import com.datang.petstore.resultpage.ResultCode;
import com.datang.petstore.service.MicroblogService;
import com.datang.petstore.service.MicroblogShareRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-11-04 10:40
 * @notify 分享接口
 * @version 1.0
 */
@Service
public class MicroblogShareRecordServiceImpl implements MicroblogShareRecordService {

    @Autowired
    private MicroblogShareRecordMapper microblogShareRecordMapper;

    @Autowired
    private MicroblogService microblogService;

    @Transactional
    public void addMicroblogShareRecord(MicroblogShareRecord microblogShareRecord) {

        //查询微博是否被删除
        Microblog microblog = microblogService.getMicroblog(microblogShareRecord.getMicroblogId());
        if (microblog.getDel().equals("1")) {
            throw new AcceptResultCodeException(ResultCode.error505);
        }


        //添加分享记录
        microblogShareRecord.setDate(new Date());
        microblogShareRecord.setMicroblogUserId(microblog.getUserId());
        microblogShareRecordMapper.insertSelective(microblogShareRecord);

        //更改微博分享次数
        microblog.setShareNumber(microblog.getShareNumber() + 1);
        microblogService.updateMicroblog(microblog);
    }
}
